package com.baidu.controller;
import com.alibaba.dubbo.config.annotation.Reference;
import com.baidu.constant.MessageConstant;
import com.baidu.entity.PageResult;
import com.baidu.entity.QueryPageBean;
import com.baidu.entity.Result;
import com.baidu.pojo.Role;
import com.baidu.pojo.User;
import com.baidu.service.UserService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 有关用户管理界面的操作
 * */
@RestController
@RequestMapping("/user")
public class UserController {

	@Reference
	private UserService userService;
	@RequestMapping("/getLoginUsername")
	public Result getLoginUsername(){
		//从session中拿出uesr对象？
		org.springframework.security.core.userdetails.User user =
				(org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext()
						.getAuthentication().getPrincipal();

		if (user!=null){
			return new Result(true, MessageConstant.GET_USERNAME_SUCCESS,user.getUsername());

		}
		return new Result(false,MessageConstant.GET_USERNAME_FAIL);

	}
	/**
	 * 分页和条件查询用户信息
	 * */
	@RequestMapping("/pageQuery")
	public PageResult pageQuery(@RequestBody QueryPageBean queryPageBean) throws Exception {
		PageResult pageResult = userService.findPage(queryPageBean);
		return pageResult;
	}
	/**
	 * 通过页面传过来的表单信息，添加用户信息
	 *
	 */

	@RequestMapping("/add")
	public Result add(@RequestBody User user){
		try {
			userService.save(user);
			return new Result(true,MessageConstant.ADD_USER_LIST_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.ADD_USER_LIST_FAIL);
		}

	}
	/**
	 *查询所有角色，展示在前端页面
	 */

	@RequestMapping("/findAllRole")
	public Result findAllRole(){
		try {
			List<Role> role = userService.showAllRole();
			return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,role);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,MessageConstant.QUERY_ROLE_FAIL);
		}

	}
	/**
	 *通过用户ID查询他所拥有的角色
	 * @param id
	 * @return
	 */

	@RequestMapping("/findRoleByUid")
	public Result findRoleByUid(Integer id){
		try {
			List<Integer> roleList = userService.showRoleById(id);
			return new Result(true,MessageConstant.QUERY_ROLE_SUCCESS,roleList);
		} catch (Exception e) {
			return new Result(true,MessageConstant.QUERY_ROLE_FAIL);
		}

	}
	/**
	 * 通过用户id和所选择的角色，修改当前用户所拥有的角色
	 * @param roleIds
	 * @param
	 */

	@PostMapping("/updateRole")
	public Result updateRoleByUid(@RequestBody User user,Integer[] roleIds){
		try {
			userService.edit(roleIds,user);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(true,MessageConstant.EDIT_ROLE_FAIL);
		}
		return new Result(true,MessageConstant.EDIT_ROLE_SUCCESS);
	}
	/**
	 *通过用户id删除指定用户,不能删除当前登录的用户
	 * @param id
	 * @return
	 */

	@RequestMapping("/deleteUser")
	public Result deleteUser(Integer id){
		org.springframework.security.core.userdetails.User user =
				(org.springframework.security.core.userdetails.User) SecurityContextHolder.getContext()
						.getAuthentication().getPrincipal();
		String loginUsername = user.getUsername();
		User user1 = userService.showUserById(id);
		String username = user1.getUsername();
		if(!loginUsername.equals(username)){
			try {
				userService.delete(id);
				return new Result(true,MessageConstant.DELETE_USER_SUCCESS);
			} catch (Exception e) {
				e.printStackTrace();
				return new Result(false,MessageConstant.DELETE_USER_FAIL);
			}
		}else {
			return new Result(false,MessageConstant.DELETE_USER_FAIL);
		}

	}

	/**
	 *通过用户id查找当前用户的信息
	 * @param id
	 * @return
	 */

	@RequestMapping("/findUserById")
	public Result findUserById(Integer id){
		try {
			User user = userService.showUserById(id);
			return new Result(true,"",user);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(false,"");
		}
	}
}
